package com.group6.exam.teacher.dao;

import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.util.DBUtil;
import com.group6.exam.teacher.entity.SubjectEntity;

public class SubjectDaoImpl implements ISubjectDao {

	@Override
	public int addSubject(SubjectEntity subject) {

		String sql = "insert into subject values(null,?)";

		return DBUtil.executeUpdate(sql, subject.getSubjectName());
	}

	@Override
	public List<Map<String, Object>> getSubjectInfo() {
		return DBUtil.executeQuery("select * from subject");
	}

	@Override
	public int deleteSubjectbyId(String subjectId) {
		return DBUtil.executeUpdate("delete from subject where subject_id=?",
				subjectId);
	}

	@Override
	public List<Map<String, Object>> getsubjectInfoById(String subjectId) {
		return DBUtil.executeQuery("select * from subject where subject_id=?",
				subjectId);
	}

	@Override
	public int updateSubjectById(SubjectEntity subjectEntity) {
		String sql = "update subject set subject_name=? where subject_id=?";
		Object[] parameters = { subjectEntity.getSubjectName(),
				subjectEntity.getSubjectId() };
		return DBUtil.executeUpdate(sql, parameters);
	}

	@Override
	public List<?> getPaging(PageModel pageModel) {
		// 获取每页第一条记录的索引(当前页-1)*每页显示数
		String sql = "select * from subject limit ?,?";
		int index = (pageModel.getCurrentPage() - 1) * pageModel.getPageSize();
		int pageSize = pageModel.getPageSize();
		return DBUtil.executeQuery(sql, index, pageSize);
	}

	@Override
	public int getTotalRecords() {
		return DBUtil.executeQuery("select * from subject").size();
	}
}
